define(["moment"], function (moment) {
	return ["$scope", "$filter", "$stateParams","$mdDialog", "config", "MMWService","$calculater",
	function ($scope, $filter, $stateParams,$mdDialog, config, $MMWService,$calculater) {
		$scope.init = function(){
            //每隻程式都有，等於window.onload方法
			//console.log($scope);
			$scope.query_info = {
				"os_no":"",
				"os_name":""
			}
			$scope.submit_info =
			{
				"os_no":"",
				"type":"",
				"subcontractor_no":"",
				"erp_no":"",
				"osdetail":{}
			}
			$scope.shipmentInfo = {};
			$scope.selectedList = [];
		}
		
		$scope.back = function(){
            //返回首頁，傳入true表示使用退出
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);//動畫
		}
		
		$scope.confirm = function(){
            //確認按鈕
		   if($scope.shipmentInfo.length <= 0 && $scope.selectedList.length <= 0)
		   {
				$scope.$root.showAlert($filter('translate')('B0701.showAlert.column4'));
				return;
		   }
		   if($scope.selectedList.length <= 0)
		   {
				$scope.$root.showAlert($filter('translate')('B0701.showAlert.column5'));
				return;
		   }
		   $mdDialog.dialog(
			'module/moduleB/program/B07/B0701/B0701_SearchPlot.tmp.html',
			storageController
			);
		};
		$scope.search = function()
		{
			if($scope.query_info.os_no == "" )
			{
				$scope.$root.showAlert($filter('translate')('B0701.showAlert.column2'));
				return;
			}
			$MMWService.sendToServer({
				uri: 'ServicesSTD.Module_WIP.os_detail_info_get',
				content:{"os_no":$scope.query_info.os_no},
				success: function (data) {
					$scope.shipmentInfo = data;
				}
			});
			$scope.selectedList = [];
		};
		$scope.itemAdd  = function(index,data)
		{
			var select_data = angular.merge({}, data);
			var find_index = -1;
			// 20210331 modify by 奕達 for M#89591
			// 20220524 modify by chenshibo for#112782：支持输入小数
			$calculater.open(function (num, dialog) {
				if((data.os_qty-num) < 0) {
					dialog.hide();
					$scope.$root.showAlert($filter('translate')('B0701.showAlert.column3')+" : "+data.os_qty);
					return ;
				}	else if (num <= 0) {
					dialog.hide();
					$scope.$root.showAlert($filter('translate')('B0701.showAlert.column8'));
					return
				} else {
					data.os_qty = data.os_qty-num;
				}
				if($scope.selectedList.length > 0)
				{
					for(var i in $scope.selectedList)
					{
						if($scope.selectedList[i].lot_no == data.lot_no)
						{
							find_index = i;
							break;
						}
					}
					if(find_index >= 0)
					{
						$scope.selectedList[find_index].os_qty =(+$scope.selectedList[find_index].os_qty)+(+num) ;
					}
					else
					{
						select_data.os_qty = (+num);
						$scope.selectedList.push(select_data);
					}
				}
				else
				{
					select_data.os_qty = (+num);
					$scope.selectedList.push(select_data);
				}
				if(data.os_qty == 0)
					$scope.shipmentInfo.osdetail.splice(index,1);
				dialog.hide();
			},
			function (dialog) {
					dialog.hide();
				},
				{
					defaultNum: data.os_qty,
					decimal: true
			});
		}
		$scope.itemDeduction  = function(index,data)
		{
			var select_data = angular.merge({}, data);
			var find_index = -1;
			if($scope.shipmentInfo.osdetail.length > 0)
			{
				for(var i in $scope.shipmentInfo.osdetail)
				{
					if($scope.shipmentInfo.osdetail[i].lot_no  == data.lot_no)
					{
						find_index = i;
						break;
					}
				}
				if(find_index >= 0)
					$scope.shipmentInfo.osdetail[find_index].os_qty =  (+$scope.shipmentInfo.osdetail[find_index].os_qty)+(+select_data.os_qty);
				else
					$scope.shipmentInfo.osdetail.push(select_data);
			}
			else
			{
				$scope.shipmentInfo.osdetail.push(select_data);
			}
			$scope.selectedList.splice(index,1);
		}
		$scope.loadOutCompanyNo = function (callback) {
			$MMWService.showQueryListPNIG({
				page_mode: 42,
				key: 'os_no',
				condition: [
					{name:$filter('translate')('B0701.dialog.column01'), column_name:'OSNO', query_mode:'0', value:$scope.query_info.os_no}
				],
				header: [
					{label:$filter('translate')('B0701.dialog.column01'), code:'os_no'},
					{label:$filter('translate')('B0701.dialog.column02'), code:'subcontractor_no'},
					{label:$filter('translate')('B0701.dialog.column03'), code:'subcontractor_name'}
				],
				//OSNO != ''
				setCondition:function(conditions){
					// 20200724 modify by WeiTing for M#75167: 拿掉 OSNO 條件，不然 Oracle 環境會查無資料。
					// conditions.push({column_name:'OSNO', query_mode:'1', value:''});
					conditions.push({column_name:'STATUS', query_mode:'0', value:'1'});
					conditions.push({column_name:'STATUS', query_mode:'0', value:'10'});
					return conditions;
				},
				confirm : function(selectItem){
					if(callback){
						callback(selectItem);
					} else {
						$scope.query_info.os_no = selectItem.os_no;
						$scope.query_info.os_name = selectItem.subcontractor_name;
					}
				}
			});
		};



		function storageController(dialog)
		{
			var controller = 
			{
			    //20220218 add by 10877 for M#107476		
			    itemCheck_change: function () {
			        dialog.Select_resubmit = false;
			        dialog.submit_info.erp_no = "";
			        dialog.submit_info.subcontractor_no = "";
			        dialog.Select_change = !dialog.Select_change;
			    },
			    //20220218 add by 10877 for M#107476
			    itemCheck_resubmit: function () {
			        dialog.Select_change = false;
			        dialog.Select_resubmit = !dialog.Select_resubmit;
			    },
				submit_info:$scope.submit_info,
				init:function()
				{
					$scope.submit_info =
					{
						"os_no":"",
						"type":"",
						"subcontractor_no":"",
						"erp_no":"",
						"osdetail":{}
					};
					dialog.submit_info = $scope.submit_info;
				},
				back: function () 
				{
					dialog.hide();
				},
				confirm: function () 
				{
					dialog.hide();
				},
				loadERP:function()
				{
				    if (dialog.Select_change != true)	//20220218 add by 10877 for M#107476
				    {
				        $scope.$root.showQueryList({
				            pageRecord: 20,
				            header: [
								{ label: $filter('translate')('B0701.queryList.column3_1'), code: 'page_no' },
								{ label: $filter('translate')('B0701.queryList.column3_2'), code: 'page_name' }
				            ],
				            query: function (conditions, feedback) {
				                $MMWService.sendToServer({
				                    uri: 'ServicesSTD.Module_SYS.erp_document_type_get',
				                    content: { doc_type: '4' },
				                    success: function (data) {
				                        feedback(data);
				                    }
				                });
				            },
				            selectCode: dialog.submit_info.erp_no,
				            confirm: function (selectItem, dialog2) {
				                dialog.submit_info.erp_no = selectItem.page_no;

				                dialog2.hide();
				            }
				        });
				    }
				},
				loadCompany:function(callback)
				{
				    if (dialog.Select_change != true)	//20220218 add by 10877 for M#107476
				    {
				        $MMWService.showQueryListPNIG({
				            page_mode: 10,
				            selectCode: dialog.submit_info.subcontractor_no,
				            condition: [
								{ name: $filter('translate')('B0701.queryList.column1_1'), column_name: 'SubcontractorNo', query_mode: '2', value: dialog.submit_info.subcontractor_no }
				            ],
				            header: [
								{ label: $filter('translate')('B0701.queryList.column1_1'), code: 'page_no' },
								{ label: $filter('translate')('B0701.queryList.column1_1'), code: 'page_name' }
				            ],
				            // setCondition:function(conditions){
				            // 	conditions.push({column_name:'a.STATUS', query_mode:'8', value:"('1','10')"});
				            // 	return conditions;
				            // },
				            confirm: function (selectItem) {
				                if (callback) {
				                    callback(selectItem);
				                } else {
				                    dialog.submit_info.subcontractor_no = selectItem.page_no;
				                }
				            }
				        });
				    }
				},
				//數量撤回
				submitByWithdraw:function()
				{	
					// 20200429刪除，modify by Oliver for M#69752 拔掉外包商設定卡控
					// if(dialog.submitCheck() == false)
					// 	return;
					dialog.submit_info.type = 1;
					dialog.submit_info.os_no = $scope.query_info.os_no;
					dialog.submit_info.osdetail = $scope.selectedList;
					$MMWService.sendToServer({
						uri: 'ServicesSTD.Module_WIP.os_detail_alter_process',
						content:dialog.submit_info,
						success: function (data) 
						{
							/*20200428 modify by Oliver for M#69752*/ 
							dialog.submitFinish();
							$scope.shipmentInfo='';
							$scope.selectedList=[];
							$scope.query_info={};
						}
					});
					
				},
				//重新出貨
				submitByReStart:function()
				{
					if(dialog.submitCheck() == false)
						return;
					dialog.submit_info.type = 0;
					dialog.submit_info.os_no = $scope.query_info.os_no;
					dialog.submit_info.osdetail = $scope.selectedList;	
					$MMWService.sendToServer({
						uri: 'ServicesSTD.Module_WIP.os_detail_alter_process',
						content:dialog.submit_info,
						success: function (data) 
						{
							dialog.submitFinish(data.sono);
							/*20200428 modify by Oliver for M#69752*/ 
							$scope.shipmentInfo='';
							$scope.selectedList=[];
							$scope.query_info={};
						}
					});
				},
				submitCheck:function()
				{
					if(dialog.submit_info.subcontractor_no == "" )
					{
						$scope.$root.showAlert($filter('translate')('B0701.showAlert.column6'));
						return false;
					}
					// if(dialog.submit_info.erp_no == "")
					// {
					// 	$scope.$root.showAlert($filter('translate')('B0701.showAlert.column7'));
					// 	return false;
					// }
					return true;
				},
				submitFinish:function()
				{
					// 20200429新增，modify by Oliver for M#69752 拔掉erp卡控送出出貨變更確定
					if(arguments[0]){
						$scope.$root.showToast($filter('translate')('B0701.showAlert.column1') +"，"+ $filter('translate')('B0701.dialog.column01') +"：" + arguments[0]);	
					}else{
						$scope.$root.showToast($filter('translate')('B0701.showAlert.column1'));	
					}
					dialog.init();
					dialog.hide();
				},
			    //20220218 add by 10877 for M#107476
				DoSubmit: function ()
				{
				    if (dialog.Select_change == true) {
				        dialog.submitByWithdraw();
				    }
				    else {
				        dialog.submitByReStart();
				    }

				}
			}
			return controller;
		};
		function pageNumberInfoGet(content, feedback) {
			$MMWService.pageNumberInfoGet(content,function (itemList)
			{
					feedback(itemList);
			});
		};
	}
]});
